package com.pz.cloud.framework.config.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.business.config.entity.BizConfig;
import com.pz.cloud.framework.business.config.service.IBizConfigService;
import com.pz.cloud.framework.business.workflow.annotation.AssembleDealFlag;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.BaseController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RequestMapping("/bizConfig")
@RestController
@Api(tags = "业务配置表")
@Slf4j
public class BizConfigController extends BaseController<BizConfig, IBizConfigService> {
    /**
     * 分页列表查询
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "业务配置-分页列表查询")
    @ApiOperation(value = "业务配置-分页列表查询", notes = "业务配置-分页列表查询")
    @GetMapping(value = "/list")
    @AssembleDealFlag
    public Result<IPage<BizConfig>> queryPageList(BizConfig entity,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        Result<IPage<BizConfig>> result = new Result<>();
        QueryWrapper<BizConfig> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        Page<BizConfig> page = new Page<>(pageNo, pageSize);
        IPage<BizConfig> pageList = service.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     * @return
     */
    @AutoLog(value = "业务配置-添加|编辑")
    @ApiOperation(value = "业务配置-添加|编辑", notes = "业务配置-添加|编辑")
    @PostMapping(value = "/add")
    public Result<BizConfig> add(@RequestBody BizConfig entity) {
        Result<BizConfig> result = new Result<>();
        try {
            service.save(entity);
            result.success("保存成功！");
        } catch (Exception e) {
            if(e instanceof CustomRuntimeException){
                result.error500(e.getMessage());
            }else{
                result.error500("操作失败");
            }
            log.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "业务配置-通过id删除")
    @ApiOperation(value = "业务配置-通过id删除", notes = "业务配置-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            service.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "业务配置-批量删除")
    @ApiOperation(value = "业务配置-批量删除", notes = "业务配置-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<BizConfig> deleteBatch(@RequestParam(name = "ids") String ids) {
        Result<BizConfig> result = new Result<BizConfig>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            service.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "业务配置-通过id查询")
    @ApiOperation(value = "业务配置-通过id查询", notes = "职务表-通过id查询")
    @GetMapping(value = "/queryById")
//    @AssembleTaskInfo
    public Result<BizConfig> queryById(@RequestParam(name = "id") String id) {
        Result<BizConfig> result = new Result<BizConfig>();
        BizConfig BizConfig = service.getById(id);
        if (BizConfig == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(BizConfig);
            result.setSuccess(true);
        }
        return result;
    }

}
